package com.smartappsnetwork.personalexpenses.dataaccess;

import java.util.Vector;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.smartappsnetwork.personalexpenses.LauncherApplication;
import com.smartappsnetwork.personalexpenses.database.DatabaseHelper;
import com.smartappsnetwork.personalexpenses.dataobject.CategoryDO;
import com.smartappsnetwork.personalexpenses.dataobject.CurrencyDO;
import com.smartappsnetwork.personalexpenses.dataobject.PC_GeneralSettingsDO;

public class CurrencyDA 
{

	public long insertCurrency(CurrencyDO currencyDO)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 
			 String query = "SELECT * FROM currency WHERE simbol=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+currencyDO.simbol});
					 if(cursor!=null && cursor.moveToNext())
					 {
//						 updateGeneralSettings(generalSettingsDO);
					 }
					 else
					 {
						 SQLiteStatement stmt    = database.compileStatement("INSERT INTO currency (simbol, name, access) VALUES (?,?,?)");
						 stmt.bindString(1, currencyDO.simbol);
						 stmt.bindString(2, currencyDO.name);
						 stmt.bindLong(3, currencyDO.access);
						 appID = stmt.executeInsert();
						 stmt.close();
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("currency", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  updateCurrency(CurrencyDO currencyDO)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement("update  currency set simbol= '"+currencyDO.simbol+"', access = access+1, name ='"+currencyDO.name+"' where currencyid="+currencyDO.currencyid);
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "currency : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	public CurrencyDO getCuurencyById(int currencyid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 CurrencyDO currencyDO = null;
			 String query = "SELECT * FROM currency WHERE currencyid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+currencyid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 currencyDO = new CurrencyDO();
							 currencyDO.currencyid = cursor.getLong(0);
							 currencyDO.simbol = cursor.getString(1);
							 currencyDO.name = cursor.getString(2);
							 currencyDO.access = cursor.getLong(3);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("category", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return currencyDO;
		}
		
	}
	
	
	public String getCurrencyNameById(int currencyid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 String name = null;
			 String query = "SELECT name FROM currency WHERE currencyid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+currencyid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 name = cursor.getString(0);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("currency", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return name;
		}
		
	}
	
	
	public String getCurrencySimbolById(int currencyid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 String simbol = null;
			 String query = "SELECT simbol FROM currency WHERE currencyid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+currencyid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 simbol = cursor.getString(0);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("currency", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return simbol;
		}
		
	}
	
	

	public Vector<CurrencyDO> getAllCurrency()
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 Vector<CurrencyDO> vecCurrencyDO = new Vector<CurrencyDO>();
			 String query = "SELECT * FROM currency";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 CurrencyDO currencyDO = new CurrencyDO();
							 currencyDO.currencyid = cursor.getLong(0);
							 currencyDO.simbol = cursor.getString(1);
							 currencyDO.name = cursor.getString(2);
							 currencyDO.access = cursor.getLong(3);
							 vecCurrencyDO.add(currencyDO);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("currency", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return vecCurrencyDO;
		}
	
		
	}
	

	public void deleteCurrencyById(int currencyid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "currencyid=?";
					 database.delete("category", whereClause, new String[]{""+currencyid});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}
	
	
	public void deleteCurrencyByName(String name)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "name=?";
					 database.delete("currency", whereClause, new String[]{""+name});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}

	
	public void deleteCurrencyBySimbol(String simbol)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "simbol=?";
					 database.delete("currency", whereClause, new String[]{""+simbol});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}

}
